package com.example.demo.service.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.constant.Status;
import com.example.demo.entity.user.UserInfo;
import com.example.demo.repository.user.UserInfoMapper;
import com.example.demo.service.user.dto.UserInfoDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

@Service  //将UserService作为bean放进spring容器里
public class UserService {



    @Autowired
    public UserInfoMapper userInfoMapper;


    public UserInfo addUser(UserInfoDto userInfoDto) {
        // 数据转换操作
        UserInfo userInfo = userInfoDto.dtoToEntity();
        userInfo.initEntity();

        Long time = System.currentTimeMillis();

        userInfo.setCreateTime(time);
        userInfo.setUpdateTime(time);
        userInfo.setBirthday("1970-01-01");


        //

        int insert = userInfoMapper.insert(userInfo);

        if (insert <= 0) {
//            throw new Exception("数据库插入数据错误");
            return null;
        }

        // 返回实体,这个实体会填充id
        return userInfo;
    }

//通过 QueryWrapper 等类可以方便地构建查询条件，替代手动编写 SQL 条件。
    public UserInfo getUserById(Long id) {
        LambdaQueryWrapper<UserInfo> qw = new LambdaQueryWrapper<>();
        qw.eq(UserInfo::getIsDelete, Status.ENABLE)
                .eq(UserInfo::getId, id);

        UserInfo userInfo = userInfoMapper.selectOne(qw);
        return Objects.isNull(userInfo) ? new UserInfo() : userInfo;
    }

    public List<UserInfo> getUsersByIds(Collection<Long> ids) {
        LambdaQueryWrapper<UserInfo> qw = new LambdaQueryWrapper<>();
        qw.eq(UserInfo::getIsDelete, Status.ENABLE)
                .in(UserInfo::getId, ids);

        List<UserInfo> userInfos = userInfoMapper.selectList(qw);
        return CollectionUtils.isEmpty(userInfos) ? Collections.EMPTY_LIST : userInfos;
    }

    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteUserById(Long id) {
        LambdaQueryWrapper<UserInfo> qw = new LambdaQueryWrapper<>();
        qw.eq(UserInfo::getIsDelete, Status.ENABLE)
                .eq(UserInfo::getId, id);
        UserInfo userInfo = userInfoMapper.selectOne(qw);
        if (Objects.isNull(userInfo)) {
            System.out.println("用户不存在,id:" + id);
        }
        // 做逻辑删除
        userInfo.setIsDelete(id);
        int i = userInfoMapper.updateById(userInfo);
        if (i <= 0) {
            System.out.println("删除失败,id:" + id);
            return Boolean.FALSE;
        }
        // 假设删除商品
        return Boolean.TRUE;
    }

    public Page<UserInfo> queryUserList(Long pageCurrent,Long pageSize) {
        LambdaQueryWrapper<UserInfo> qw = new LambdaQueryWrapper<>();
        qw.eq(UserInfo::getIsDelete, Status.ENABLE);

        Page<UserInfo> page = new Page<UserInfo>(pageCurrent, pageSize);

        Page<UserInfo> userInfoPage = userInfoMapper.selectPage(page, qw);

        return userInfoPage;

    }
}
